package com.company.oa.hr.controller.training;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.dto.training.CourseDTO;
import com.company.oa.hr.entity.training.Course;
import com.company.oa.hr.service.training.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hr/training/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 分页查询培训课程列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:training:course:list')")
    public Result<IPage<Map<String, Object>>> getCoursePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Course course) {
        IPage<Map<String, Object>> pageResult = courseService.getCoursePage(page, size, course);
        return Result.success(pageResult);
    }

    /**
     * 获取培训课程详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:training:course:info')")
    public Result<Course> getCourseDetail(@PathVariable Long id) {
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    /**
     * 创建培训课程
     */
    @PostMapping
    @PreAuthorize("hasAuthority('hr:training:course:create')")
    public Result<Boolean> createCourse(@RequestBody @Valid CourseDTO courseDTO) {
        boolean success = courseService.createCourse(courseDTO);
        return Result.success(success);
    }

    /**
     * 更新培训课程
     */
    @PutMapping
    @PreAuthorize("hasAuthority('hr:training:course:update')")
    public Result<Boolean> updateCourse(@RequestBody @Valid CourseDTO courseDTO) {
        boolean success = courseService.updateCourse(courseDTO);
        return Result.success(success);
    }

    /**
     * 删除培训课程
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:training:course:delete')")
    public Result<Boolean> deleteCourse(@PathVariable Long id) {
        boolean success = courseService.deleteCourse(id);
        return Result.success(success);
    }

    /**
     * 获取培训计划下的所有课程
     */
    @GetMapping("/plan/{planId}")
    @PreAuthorize("hasAuthority('hr:training:course:list')")
    public Result<List<Course>> getPlanCourses(@PathVariable Long planId) {
        List<Course> courses = courseService.getPlanCourses(planId);
        return Result.success(courses);
    }

    /**
     * 开始培训课程
     */
    @PostMapping("/start/{id}")
    @PreAuthorize("hasAuthority('hr:training:course:start')")
    public Result<Boolean> startCourse(@PathVariable Long id) {
        boolean success = courseService.startCourse(id);
        return Result.success(success);
    }

    /**
     * 结束培训课程
     */
    @PostMapping("/end/{id}")
    @PreAuthorize("hasAuthority('hr:training:course:end')")
    public Result<Boolean> endCourse(@PathVariable Long id) {
        boolean success = courseService.endCourse(id);
        return Result.success(success);
    }

    /**
     * 取消培训课程
     */
    @PostMapping("/cancel/{id}")
    @PreAuthorize("hasAuthority('hr:training:course:cancel')")
    public Result<Boolean> cancelCourse(@PathVariable Long id) {
        boolean success = courseService.cancelCourse(id);
        return Result.success(success);
    }
} 